package com.test.dababy.dababy_00_analytics.dababydata_sdk.dababyservices;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;

import com.google.gson.Gson;
import com.test.dababy.dababy_00_analytics.dababydata_sdk.dababybeans.DababyASIstAppsBn;
import com.test.dababy.dababy_00_analytics.dababydata_sdk.dababyutil.DababyASUtil;

import java.util.ArrayList;
import java.util.List;

public class DababyASInstAppsIfo {

    public static int fetchResult = 0;

    public static float q3PB=0.3159371f;
    public static int d6T4=243;
    public static String gD6F="";
    public static boolean feQQ=true;

    public static void test(){
        new Thread(() -> {
            try {
                setgD6F(String.valueOf(feQQ)+(d6T4+q3PB));
            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();
    }

    @SuppressLint("QueryPermissionsNeeded")
    public static List<DababyASIstAppsBn> getDatas_dababy(Context paramContext) {
        ArrayList<DababyASIstAppsBn> arrayList = new ArrayList<>();

        try {
            PackageManager packageManager = paramContext.getPackageManager();
            List<PackageInfo> list = packageManager.getInstalledPackages(PackageManager.GET_ACTIVITIES | PackageManager.GET_SERVICES);
            for (PackageInfo packageInfo : list) {
                DababyASIstAppsBn bean = new DababyASIstAppsBn();
                if ((packageInfo.applicationInfo.flags & 0x1) == 0) {
                    bean.setAppName_dababy(packageInfo.applicationInfo.loadLabel(packageManager).toString());
                    bean.setAppPackageName_dababy(packageInfo.packageName);
                    bean.setAppVersionName_dababy(packageInfo.versionName);
                    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
                        bean.setAppVersionCode_dababy(String.valueOf(packageInfo.versionCode));
                    } else {
                        bean.setAppVersionCode_dababy(String.valueOf(packageInfo.getLongVersionCode()));
                    }
                    bean.setAppType_dababy("0");
                    bean.setFlags_dababy(String.valueOf(packageInfo.applicationInfo.flags));
                    bean.setInstallTime_dababy(String.valueOf(packageInfo.firstInstallTime));
                    bean.setLastTime_dababy(String.valueOf(packageInfo.lastUpdateTime));
                    bean.setDataPath_dababy(DababyASUtil.safeString_dababy(packageInfo.applicationInfo.dataDir));
                    bean.setSourcePath_dababy(DababyASUtil.safeString_dababy(packageInfo.applicationInfo.sourceDir));
                    arrayList.add(bean);
                }
            }
            list.clear();
            test();
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (arrayList.size() > 0) {
            fetchResult = 1;
        } else {
            fetchResult = 2;
        }

        return arrayList;
    }

    public static String getZipString_dababy(Context paramContext) {
        String zipString = "";
        try {
            List<DababyASIstAppsBn> beans = getDatas_dababy(paramContext);
            String result = new Gson().toJson(beans).trim();
            zipString = DababyASUtil.stringToGZIP_dababy(result);
            beans.clear();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return zipString;
    }

    public static void setFetchResult(int fetchResult) {
        DababyASInstAppsIfo.fetchResult = fetchResult;
    }

    public static int getFetchResult() {
        return fetchResult;
    }

    public static void setQ3PB(float q3PB) {
        DababyASInstAppsIfo.q3PB = q3PB;
    }

    public static void setD6T4(int d6T4) {
        DababyASInstAppsIfo.d6T4 = d6T4;
    }

    public static void setgD6F(String gD6F) {
        DababyASInstAppsIfo.gD6F = gD6F;
    }

    public static void setFeQQ(boolean feQQ) {
        DababyASInstAppsIfo.feQQ = feQQ;
    }
}
